// @ts-ignore
import { Effect, Reducer, Subscription, request } from 'umi';

export interface DjModelState {
  categories: [];
  recommend: [];
}

export interface DjModelType {
  namespace: 'djModel';
  state: DjModelState;
  effects: any;
  reducers: any;
  subscriptions: any;
}

const djModel: DjModelType = {
  namespace: 'djModel',

  state: {
    categories: [],
    recommend: [],
  },

  effects: {
    *getCategories({ type, payload }: any, { put, call, select }: any) {
      let data = yield request(`/dj/catelist`);
      yield put({
        type: 'saveCategories',
        payload: {
          categories: data.categories,
        },
      });
    },
    *getRecommend({ type, payload }: any, { put, call, select }: any) {
      let data = yield request(`/dj/recommend/type?type=${payload.id}`);
      yield put({
        type: 'saveRecommend',
        payload: {
          recommend: data.djRadios,
        },
      });
    },
  },
  reducers: {
    saveCategories(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
    saveRecommend(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {},
};

export default djModel;
